﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExcelWritter
{
    public partial class Form1 : Form
    {
        private SimpleExcelWritter excel = null;
        /// <summary>
        /// Excel相关操作组建是否可交互
        /// </summary>
        public bool ExcelSwitch
        {
            get
            {
                return inBox.Enabled;
            }
            set
            {
                if (value)
                    status.Text = "ON";
                else
                    status.Text = "OFF";
                inBox.Enabled = value;
                showExcelButton.Enabled = value;
                if (value == false)
                    hideExcelButton.Enabled = value;
                submitButton.Enabled = value;
                upButton.Enabled = value;
                downButton.Enabled = value;
                leftButton.Enabled = value;
                rightButton.Enabled = value;
                launchButton.Enabled = !value;
                closeButton.Enabled = value;
            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (excel != null)
            {
                excel.ExcelClose();
                excel = null;
            }
            GC.Collect();
            Environment.Exit(0);
        }

        private void launchButton_Click(object sender, EventArgs e)
        {
            excel = new SimpleExcelWritter();
            ExcelSwitch = true;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            excel.ExcelClose();
            excel = null;
            GC.Collect();
            ExcelSwitch = false;
        }

        private void showExcelButton_Click(object sender, EventArgs e)
        {
            excel.ExcelAppVisible = true;
            hideExcelButton.Enabled = true;
            showExcelButton.Enabled = false;
        }

        private void hideExcelButton_Click(object sender, EventArgs e)
        {
            excel.ExcelAppVisible = false;
            hideExcelButton.Enabled = false;
            showExcelButton.Enabled = true;
        }

        private void ReloadExcelLabels()
        {
            rowValueLabel.Text = excel.intIndex + "";
            rankValueLabel.Text = excel.stringIndex;
            inBox.Text = excel.Read();
        }

        private void upButton_Click(object sender, EventArgs e)
        {
            excel.Up();
            ReloadExcelLabels();
        }

        private void downButton_Click(object sender, EventArgs e)
        {
            excel.Down();
            ReloadExcelLabels();
        }

        private void leftButton_Click(object sender, EventArgs e)
        {
            excel.Left();
            ReloadExcelLabels();
        }

        private void rightButton_Click(object sender, EventArgs e)
        {
            excel.Right();
            ReloadExcelLabels();
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            excel.Write(inBox.Text);
        }
    }
}
